package com.cs.study.homework._0308;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class Demo4 {
    public static void main(String[] args) throws ParseException {
        //创建计时器
        Timer timer = new Timer();

        timer.schedule(new MyTask(), 3000, 5000);


        //终止计时器
        try {
            //20s后终止
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        timer.cancel();

    }


}
class MyTask extends TimerTask{
    //设定时间格式
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void run() {
        String date = sdf.format(new Date());
        System.out.println(date);
    }

}
